<?php
define('DEBUG', false);											// Debug mode
define('PS_SHOP_PATH', 'http://www.phonecollection.com.au/shopping/');							// Root path of your PrestaShop store
define('PS_WS_AUTH_KEY', 'KU5BBFBKMLNXXO2F8DEK0NJITQHLFX93');

require_once('PSWebServiceLibrary.php');
class Model_Ps_Orderstatusupdate {
	
	
	public static function updateStatus($idOrder,$idStatus){
		$resources = '';
		$xml = '';
		try{
			$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
			$opt['resource'] = 'orders';
			$opt['id'] = (int)$idOrder;
			$xml = $webService->get($opt);
			$resources = $xml->children()->children();
		}
		catch (PrestaShopWebserviceException $e)
		{
			// Here we are dealing with errors
			$trace = $e->getTrace();
			if ($trace[0]['args'][0] == 404) echo 'Bad ID';
			else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
			else echo 'Other error<br />'.$e->getMessage();
		}
		// get current status 
		
		$resources->current_state = $idStatus;
		
		try{
			$opt = array('resource' => 'orders');
			$opt['putXml'] = $xml->asXML();
			$opt['id'] = $idOrder;
			$xml = $webService->edit($opt);
		
		}
		
		catch (PrestaShopWebserviceException $e)
		{
			// Here we are dealing with errors
			$trace = $e->getTrace();
			if ($trace[0]['args'][0] == 404) echo 'Bad ID';
			else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
			else echo 'Other error<br />'.$e->getMessage();
		}
		
		
	}
	
	public static function updateStatusHistory($idOrder,$idStatus){
		
		$resource = '';
		try{
			$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
			$xmlHistory = $webService->get(array('url'=>PS_SHOP_PATH.'/api/order_histories?schema=blank'));
			$resources = $xmlHistory->children()->children();
			//$resources->id = 1213;
			$resources->id_order_state = $idStatus;
			$resources->id_order = $idOrder;
			$resources->id_employee = 1;
			$resources->date_add = Model_DatetimeHelper::dateToday().' '.Model_DatetimeHelper::timeNow();
			
			$optAdd = array('resource' => 'order_histories?sendemail=1');
			$optAdd['postXml'] = $xmlHistory->asXML();
			$xmlHistoryRes = $webService->add($optAdd);
		}
		catch (PrestaShopWebserviceException $e)
		{
			$trace = $e->getTrace();
			if ($trace[0]['args'][0] == 404) echo 'Bad ID';
			else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
			else echo 'Other error<br />'.$e->getMessage();
		}
		
		
		
	}
	

	public function getCurrentStatus(){
	/*		
	$resources = '';	
	try
{
	$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
	// Here we set the option array for the Webservice : we want customers resources
	$opt['resource'] = 'orders';
	// We set an id if we want to retrieve infos from a customer
	if (isset($_GET['id']))
		$opt['id'] = (int)$_GET['id']; // cast string => int for security measures

	// Call
	$xml = $webService->get($opt);

	// Here we get the elements from children of customer markup which is children of prestashop root markup
	$resources = $xml->children()->children();
}
catch (PrestaShopWebserviceException $e)
{
	// Here we are dealing with errors
	$trace = $e->getTrace();
	if ($trace[0]['args'][0] == 404) echo 'Bad ID';
	else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
	else echo 'Other error<br />'.$e->getMessage();
}

	d($resources);
	$resources->current_state = 14;
	
	
	try{
		$opt = array('resource' => 'orders');
		$opt['putXml'] = $xml->asXML();
		$opt['id'] = $_GET['id'];
		
		d($opt);
		$xml = $webService->edit($opt);
		
	}
	
	catch (PrestaShopWebserviceException $e)
	{
		// Here we are dealing with errors
		$trace = $e->getTrace();
		if ($trace[0]['args'][0] == 404) echo 'Bad ID';
		else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
		else echo 'Other error<br />'.$e->getMessage();
	}

	//echo trim($resources->current_status);

	// We set an id if we want to retrieve infos from a customer
try{
	$xmlHistory = $webService->get(array('url'=>PS_SHOP_PATH.'/api/order_histories?schema=blank'));
	$resources = $xmlHistory->children()->children();
		//$resources->id = 1213;
		$resources->id_order_state = 14;
		$resources->id_order = 608;
		$resources->id_employee = 1;
		$resources->date_add = Model_DatetimeHelper::dateToday().' '.Model_DatetimeHelper::timeNow();
		
		d($xmlHistory);
		
	$optAdd = array('resource' => 'order_histories');
	$optAdd['postXml'] = $xmlHistory->asXML();
	$xmlHistoryRes = $webService->add($optAdd);	
	d($xmlHistoryRes);
}
catch (PrestaShopWebserviceException $e)
{
	// Here we are dealing with errors
	$trace = $e->getTrace();
	if ($trace[0]['args'][0] == 404) echo 'Bad ID';
	else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
	else echo 'Other error<br />'.$e->getMessage();
	
	d($trace);
}

*/	
	}
	
}

?>